package cn.hiapi.shopkeeper.entity.activity;

import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.serializer.LongToStringSerializer;
import cn.hiapi.shopkeeper.enums.ActivityType;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.math.BigDecimal;

/**
 * @author AdinZ
 * @date 2022/12/13 0:04
 */
@Entity
@Table(name = "hiapi_shopkeeper_activity")
@ApiModel(description = "活动主题")
@Getter
@Setter
public class Activity extends BasicEntity {
    @Id
    @GeneratedValue(generator = "snowflake")
    @GenericGenerator(name = "snowflake", strategy = "cn.hiapi.basic.id.SnowflakeId")
    @JSONField(serializeUsing = LongToStringSerializer.class)
    private Long id;
    @JSONField(serializeUsing = LongToStringSerializer.class)
    private long cid;
    @ApiModelProperty("活动id")
    @Enumerated(EnumType.STRING)
    private ActivityType type;

    @ApiModelProperty("活动封面")
    private String thumb;

    @ApiModelProperty("活动标题")
    private String title;
    @ApiModelProperty("活动介绍")
    private String body;
    @ApiModelProperty("店铺id")
    private long shopId;
    @ApiModelProperty("开始时间")
    private long startTime;
    @ApiModelProperty("结束时间")
    private long endTime;

    @ApiModelProperty("使用开始时间")
    private long useStart;
    @ApiModelProperty("使用结束时间")
    private long useEnd;


    @ApiModelProperty("原价")
    @Column(columnDefinition = "decimal(10,2)")
    private BigDecimal price = BigDecimal.ZERO;

    @ApiModelProperty("创建时间")
    private long created = System.currentTimeMillis();

    @ApiModelProperty("更新时间")
    private long updated = System.currentTimeMillis();


    @ApiModelProperty("状态,0 未开始,1进行中,2已结束")
    private int status;

    @ApiModelProperty("兑换时间状态")
    private int useStatus;


    public String verifyStatus() {
        if (status == 0) return "活动未开始";
        if (status == 2 && endTime > System.currentTimeMillis()) return "活动已暂停";
        if (status == 2) return "活动已结束";
        if (startTime > System.currentTimeMillis()) return "活动还没开始";
        if (endTime < System.currentTimeMillis()) return "活动已结束";
        if (useEnd < System.currentTimeMillis()) return "活动兑换已结束";
        return null;
    }


    /**
     * 验证状态
     *
     * @return
     */
    public void verifyStatusThr() {
        String s = this.verifyStatus();
        if (StringUtils.isNotEmpty(s)) {
            throw new BasicException(s);
        }
    }


}
